package com.jianyun.jyzs.fragment;


import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.jianyun.jyzs.R;
import com.jianyun.jyzs.activity.SetMyProfileActivity;
import com.jianyun.jyzs.activity.SetPasswordActivity;
import com.jianyun.jyzs.activity.SettingActivity;
import com.jianyun.jyzs.activity.UpdataEmailActivity;
import com.jianyun.jyzs.activity.UpdatePhoneActivity;
import com.jianyun.jyzs.base.MVPBaseFragment;
import com.jianyun.jyzs.bean.MeUserInfo;
import com.jianyun.jyzs.common.IntentRequestCode;
import com.jianyun.jyzs.constant.SysConstant;
import com.jianyun.jyzs.presenter.MeFragmentPresenter;
import com.jianyun.jyzs.utils.LoginCache;
import com.jianyun.jyzs.utils.PhotoUtils;
import com.jianyun.jyzs.utils.UploadPortraitUtil;
import com.jianyun.jyzs.view.iview.IMeFragmentView;
import com.jianyun.jyzs.widget.LoadingDialog;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

import static android.app.Activity.RESULT_OK;

/**
 * 我的 fragment
 * Created by yangshijun on 2018/5/25.
 */

public class MeFragment extends MVPBaseFragment<IMeFragmentView,
        MeFragmentPresenter> implements IMeFragmentView {

    @BindView(R.id.head)
    ImageView head;
    @BindView(R.id.name)
    TextView name;
    @BindView(R.id.subName)
    TextView subName;
    @BindView(R.id.update_name)
    LinearLayout updateName;
    @BindView(R.id.order2)
    LinearLayout order2;
    @BindView(R.id.tv_sex)
    TextView tvSex;
    @BindView(R.id.sex)
    TextView sex;
    @BindView(R.id.update_sex)
    LinearLayout updateSex;
    @BindView(R.id.tv_email)
    TextView tvEmail;
    @BindView(R.id.email)
    TextView email;
    @BindView(R.id.update_email)
    LinearLayout updateEmail;
    @BindView(R.id.tv_phone)
    TextView tvPhone;
    @BindView(R.id.phone)
    TextView phone;
    @BindView(R.id.update_phone)
    LinearLayout updatePhone;
    @BindView(R.id.tv_company)
    TextView tvCompany;
    @BindView(R.id.company)
    TextView company;
    @BindView(R.id.update_company)
    LinearLayout updateCompany;
    @BindView(R.id.tv_department)
    TextView tvDepartment;
    @BindView(R.id.department)
    TextView department;
    @BindView(R.id.update_department)
    LinearLayout updateDepartment;
    @BindView(R.id.update_password)
    RelativeLayout updatePassword;
    @BindView(R.id.update_set)
    RelativeLayout updateSet;

    Unbinder unbinder;
    public LoadingDialog loadingDialog;
    public UploadPortraitUtil uploadPortrait;
    public MeUserInfo userinfo;
    private String userId;
    private String enterpriseCode;


    @Override
    protected void initView() {
        loadingDialog = LoadingDialog.create(getContext());
        initUploadPortrait();
    }

    @Override
    protected void initData() {
        userId = LoginCache.getInstance().getUserInfo().getUserId();

        enterpriseCode = LoginCache.getInstance().getLoginCache().getEnterpriseCode();

        getPresenter().getUserInfo(enterpriseCode, userId);

    }

    @Override
    protected int initContentLayout() {
        return R.layout.rce_fragment_me_2;
    }

    @Override
    public MeFragmentPresenter createPresenter() {
        return new MeFragmentPresenter();
    }


    @Override
    public void onResume() {
        super.onResume();
        try {
            //收到
//            String iconPaths = LoginCache.getInstance().getUserDetaileInfo().getIconPaths();
//            if (iconPaths != null && iconPaths.length() > 25) {
//                String erpRootUrl = LoginCache.getInstance().getLoginCache().getErpRootUrl();
//                String url = erpRootUrl + "SCSEXEC/BrowseImage.aspx?width=180&height=180&" + iconPaths;
//                Log.i("test", "路径：" + url);
//                // Glide.with(this).load(url).into(imgUserPortrait);
//            }
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    @OnClick({R.id.update_set, R.id.head, R.id.update_phone, R.id.update_email, R.id.update_password})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.update_set:
                startActivity(new Intent(getActivity(), SettingActivity.class));
                break;
            case R.id.head:
                uploadPortrait.showPhotoDialog();
                break;
            case R.id.update_phone:
                startActivityForResult(new Intent(getActivity(), UpdatePhoneActivity.class).putExtra(SysConstant.msg_key_string1, phone.getText()), 101);
                break;
            case R.id.update_email:
                startActivityForResult(new Intent(getActivity(), UpdataEmailActivity.class).putExtra(SysConstant.msg_key_string1, email.getText()), 101);
                break;
            case R.id.update_password:
                startActivity(new Intent(getActivity(), SetPasswordActivity.class));
                break;
            default:
                break;

        }
    }

    private void initUploadPortrait() {
        //修改头像
        uploadPortrait = new UploadPortraitUtil(getActivity(), loadingDialog);
        uploadPortrait.setUploadPortraitListener(new UploadPortraitUtil.onUploadPortraitListener() {
            @Override
            public void onUploadSuccess(final String thumbUrl, final String largeUrl) {
                String erpRootUrl = LoginCache.getInstance().getLoginCache().getErpRootUrl();
                String url = erpRootUrl + largeUrl;
                Log.i("test", "修改头像后路径：" + url);
                Glide.with(getActivity()).load(url).into(head);
            }

            @Override
            public void onUploadFailed(String msg) {
                Log.i("test", "照片：" + msg);
            }
        });
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (requestCode) {
            case PhotoUtils.INTENT_CROP:
                if (resultCode == RESULT_OK) {
                    loadingDialog.show();
                }
                uploadPortrait.getPhotoUtils().onActivityResult(getActivity(), requestCode, resultCode, data);
                break;
            case PhotoUtils.INTENT_TAKE:
            case PhotoUtils.INTENT_SELECT:
                uploadPortrait.getPhotoUtils().onActivityResult(getActivity(), requestCode, resultCode, data);
                break;
            case 101:
                getPresenter().getUserInfo(enterpriseCode, userId);
                break;
        }
    }


    @Override
    public void setUserinfo(MeUserInfo userinfo) {
        this.userinfo = userinfo;
        name.setText(userinfo.getName());
        subName.setText(userinfo.getDepartment());
        sex.setText(userinfo.getSex());
        email.setText(userinfo.getEmail());
        phone.setText(userinfo.getPhoneNum());
        company.setText(userinfo.getCompany());
        department.setText(userinfo.getDepartment());
        String erpRootUrl = LoginCache.getInstance().getLoginCache().getErpRootUrl();
        String url = erpRootUrl + userinfo.getIconPaths();
        Log.i("test", "路径：" + url);
        Glide.with(this).load(url).into(head);
    }

    @Override
    public void onFailed() {

    }

    @Override
    public void onException(String msg) {

    }

}
